package linechart;

import common.ChartDemo;
import common.ChartUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class LineChartDemo3 extends ChartDemo {

    public LineChartDemo3(String title) {
        super(title);
    }

    @Override
    protected Dataset createDataset() {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        for (int i = 0; i < 10; i++) {
            XYSeries xySeries = new XYSeries((new StringBuilder()).append("S").append(i).toString());
            for (int j = 0; j < 10; j++) {
                xySeries.add(j, Math.random() * 100D);
            }
            xySeriesCollection.addSeries(xySeries);
        }
        return xySeriesCollection;
    }

    @Override
    protected JFreeChart createChart(Dataset dataset) {
        XYDataset xyDataset = (XYDataset) dataset;
        JFreeChart jFreeChart = ChartFactory.createXYLineChart("Line Chart Demo 3", "X",
                "Y", xyDataset, PlotOrientation.VERTICAL,
                true, true, false);

        XYPlot xyPlot = (XYPlot) jFreeChart.getPlot();
        xyPlot.setDomainPannable(true);
        xyPlot.setRangePannable(true);
        xyPlot.setDomainZeroBaselineVisible(true);
        xyPlot.setRangeZeroBaselineVisible(true);

        XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyPlot.getRenderer();
        xyLineAndShapeRenderer.setDefaultShapesVisible(true);
        xyLineAndShapeRenderer.setDefaultShapesFilled(true);
        xyLineAndShapeRenderer.setDrawOutlines(true);

        NumberAxis numberAxis = (NumberAxis) xyPlot.getRangeAxis();
        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        return jFreeChart;
    }

    public static void main(String[] args) {
        LineChartDemo3 demo1 = new LineChartDemo3("LineChartDemo3");
        ChartUtil.showChartDemo(demo1);
    }
}
